﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BDS3M.Admin.Controls
{
    public partial class EditProfileLogin : System.Web.UI.UserControl
    {
        public string user;
        public string email;
        public string fullname;
        public string phone;
        public string address;
        public string avatar;
        public string role;
        public int qlsanpham;
        public int qldanhmuc;
        public int qlhoadon;
        public int qlkhachhang;
        public int qlgiaodien;
        public int qlttamark;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["idaccount"] != null)
                {
                    int id = Convert.ToInt32(Request.QueryString["idaccount"]);
                    DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_Account", "Account_ID", id.ToString());
                    user = dt.Rows[0]["Account_User"].ToString();
                    email = dt.Rows[0]["Account_Email"].ToString();
                    fullname = dt.Rows[0]["Account_FullName"].ToString();
                    phone = dt.Rows[0]["Account_Phonenumber"].ToString();
                    address = dt.Rows[0]["Account_Address"].ToString();
                    avatar = dt.Rows[0]["Account_Avatar"].ToString();
                    role = dt.Rows[0]["Account_Role"].ToString();

                    txtEmail.Text = email;
                    txtFullname.Text = fullname;
                    txtPhone.Text = phone;
                    txtUsername.Text = user;

                    txtAddress.Text = address;
                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {

            int user_id = Convert.ToInt32(Request.QueryString["idaccount"]);
            string email = txtEmail.Text;
            string fullname = txtFullname.Text;
            string address = txtAddress.Text;
            string mobile = txtPhone.Text;
            string filePath = null;
            string username = txtUsername.Text;
            DataTable checkuseremail = DAL.DCollector.DBCollector.Instance.CallStored("callstored", "SELECT count (*) as result FROM (SELECT *   FROM tbl_Account where Account_ID <> " + user_id + ") tbl WHERE Account_User = '" + username + "' OR Account_Email = '" + email + "'", true);
            if (checkuseremail.Rows[0]["result"].ToString() == "0")
            {
                if (fulAvatar.HasFile)
                {
                    if (LIB.Validate.CheckFileType(fulAvatar.FileName))
                    {
                        string link = fulAvatar.FileName;
                        LIB.Validate saveimage = new LIB.Validate();
                        saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Resources/Images/Images-Account/"), link, 250, fulAvatar.PostedFile.InputStream);
                        filePath = "/Resources/Images/Images-Account/" + fulAvatar.FileName;
                        if (avatar == "")
                        {
                            DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[6];
                            cl[0] = new DAL.DHelpers.Column("Account_User", username, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[1] = new DAL.DHelpers.Column("Account_Email", email, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[2] = new DAL.DHelpers.Column("Account_FullName", fullname, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[3] = new DAL.DHelpers.Column("Account_Address", address, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[4] = new DAL.DHelpers.Column("Account_Phonenumber", mobile, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[5] = new DAL.DHelpers.Column("Account_Avatar", filePath, DAL.DHelpers.DataType.DataTypeSql.Varchar);

                            string[] colWhere = new string[1];
                            colWhere[0] = "Account_ID";

                            object[] colValue = new object[1];
                            colValue[0] = user_id;

                            DAL.DCollector.DBCollector.Instance.Update("tbl_Account", cl, colWhere, colValue);
                            Response.Redirect("/Admin/Index.aspx?panel=0&sub=7&idaccount=" + user_id);

                        }
                        else
                        {
                            File.Delete(MapPath(avatar));
                            DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[6];
                            cl[0] = new DAL.DHelpers.Column("Account_User", username, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[1] = new DAL.DHelpers.Column("Account_Email", email, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[2] = new DAL.DHelpers.Column("Account_FullName", fullname, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[3] = new DAL.DHelpers.Column("Account_Address", address, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[4] = new DAL.DHelpers.Column("Account_Phonenumber", mobile, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[5] = new DAL.DHelpers.Column("Account_Avatar", filePath, DAL.DHelpers.DataType.DataTypeSql.Varchar);

                            string[] colWhere = new string[1];
                            colWhere[0] = "Account_ID";

                            object[] colValue = new object[1];
                            colValue[0] = user_id;

                            DAL.DCollector.DBCollector.Instance.Update("tbl_Account", cl, colWhere, colValue);
                            Response.Redirect("/Admin/Index.aspx?panel=0&sub=7&idaccount=" + user_id);
                        }
                    }
                    else
                    {
                        lblCheckFileImages.Text = "Chỉ có thể upload file(.jpg,.png,.jpeg)";
                    }
                }
                else
                {
                    DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[5];
                    cl[0] = new DAL.DHelpers.Column("Account_User", username, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                    cl[1] = new DAL.DHelpers.Column("Account_Email", email, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                    cl[2] = new DAL.DHelpers.Column("Account_FullName", fullname, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                    cl[3] = new DAL.DHelpers.Column("Account_Address", address, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                    cl[4] = new DAL.DHelpers.Column("Account_Phonenumber", mobile, DAL.DHelpers.DataType.DataTypeSql.Varchar);

                    string[] colWhere = new string[1];
                    colWhere[0] = "Account_ID";

                    object[] colValue = new object[1];
                    colValue[0] = user_id;

                    DAL.DCollector.DBCollector.Instance.Update("tbl_Account", cl, colWhere, colValue);
                    Response.Redirect("/Admin/Index.aspx?panel=0&sub=7&idaccount=" + user_id);
                }
            }
            else
            {
                Response.Write("<script>alert('Tài khoản hoặc Email này đã tồn tại...')</script>");
            }

        }
    }
}